<?php

namespace App\Admin\Controllers;

use App\Models\Gamelog;
use App\Models\School;

use App\Models\Traits\GameTypeTrait;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;

class SchoolsController extends Controller
{
    use ModelForm, GameTypeTrait;
    protected $id;
    public $game_list = [
        'typhoon' => [
            'correct_count' => 0,
        ],
        'earthquake' => [
            'correct_count' => 0,
        ],
        'etiquette' => [
            'correct_count' => 0,
        ],
        'fire'      => [
            'correct_count' => 0,
        ]
    ];
    public function show($id)
    {
        $this->id = $id;
        return Admin::content(function (Content $content)  {

            $content->header(School::find($this->id)->name);
            $content->description('Description...');

            $content->row('基本统计信息');

            $content->row(function (Row $row){

                $row->column(6, function (Column $column)  {
                    $name = 'typhoon';
                    $title = '台风题库正确率统计';
                   $this->calculateList($name);
                    $correct_count = $this->handleAnswers($this->game_list[$name]);
                    $sum = 100;

                    $column->append(view('admin.charts.doughnut', compact(['correct_count', 'sum', 'name', 'title'])));
                });

                $row->column(6, function (Column $column) {
                    $title = '地震题库正确率统计';
                    $name = 'earthquake';
                    $this->calculateList($name);
                    $correct_count = $this->handleAnswers($this->game_list[$name]);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut', compact(['correct_count', 'sum', 'name', 'title'])));
                });
            });
            $content->row(function (Row $row) {

                $row->column(6, function (Column $column) {
                    $title = '八礼题库正确率统计';
                    $name = 'etiquette';
                    $this->calculateList($name);
                    $correct_count = $this->handleAnswers($this->game_list[$name]);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut', compact(['correct_count', 'sum', 'name', 'title'])));
                });

                $row->column(6, function (Column $column) {
                    $title = '火灾题库正确率统计';
                    $name = 'fire';
                    $this->calculateList($name);
                    $correct_count = $this->handleAnswers($this->game_list[$name]);
                    $sum = 100;
                    $column->append(view('admin.charts.doughnut', compact(['correct_count', 'sum', 'name', 'title'])));
                });
            });
        });
    }

    private function calculateList($game)
    {
        $questions = Gamelog::where(['school_id' => $this->id, 'game_type' => $game])->get(['game_data']);
        $data = 0;
        if($questions){
            foreach ($questions as $question)
            {
                if($question['game_data']){
                    $this->handleDataList($question['game_data'], $game);
                 }
            }
        }
    }

    public function handleDataList($game_data, $game)
    {
        //解析游戏记录中的数据列表
        $log_list = json_decode($game_data, true);

        foreach ($log_list as $data)
        {
            $this->handleData($data, $game);
        }

    }



    public function handleData($data, $name)
    {
        $game = $this->game_types[$name]::find($data['question_id']);
        $list = [];
        if($game){
            //初始化数据记录
            $answers = explode("|", $game->answers);
            $count = count($answers);

            for($i = 1; $i <= $count; $i ++)
            {
                $list[$this->options[$i]] = 0;
            }
        }
        foreach($list as $key => $value)
        {
            if($data['currentChoose'] == $key)
            {
                $list[$key] ++;
            }

        }
        if ($data['isCorrect'] == 1)
        {
            $this->game_list[$name]['correct_count'] ++;
        }
        if (!isset($this->game_list[$name][$data['question_id']])){
            $this->game_list[$name][$data['question_id']] = [];
        }
        array_push($this->game_list[$name][$data['question_id']], $list);
    }

    public function handleAnswers($list)
    {
       $sum = 0;
       if(count($list) == 1){
           return 0;
       }
        foreach ($list as $value)
        {
            if (is_array($value)){
                $sum += count($value);
            }
        }

        return round($list['correct_count'] / $sum * 100) ;
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('学校列表');
            $content->description('管理学校的分类');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改学校的名称');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建新的学校分类');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(School::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $id = $actions->getKey();
                $actions->append('<a href="./schools/'.$id.'"><button class="btn btn-info btn-sm">统计信息</button></a>');
            });
            $grid->id('ID')->sortable();
            $grid->column('name', '学校名称');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(School::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '学校名称');
        });
    }
}
